package com.dhcc.bpm.modules.metadata.wrapper;

import com.dhcc.bpm.modules.metadata.entity.ApiMetadata;
import com.dhcc.bpm.modules.metadata.service.impl.ApiMetadataServiceImpl;
import com.dhcc.bpm.modules.metadata.vo.ApiMetadataVO;
import com.dhcc.bpm.modules.metadata.vo.MetadataVO;
import com.dhcc.bpm.modules.metadata.vo.ServerApiMetadataVO;
import org.springblade.core.mp.support.BaseEntityWrapper;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.node.ForestNodeMerger;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @ClassName MetadataWrapper
 * @Description 包装类,返回视图层所需的字段
 * @Author daina
 * @Date 2020/2/21 14:16
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 */
public class ApiMetadataWrapper extends BaseEntityWrapper<ApiMetadata, ApiMetadataVO> {
	@Autowired
	private ApiMetadataServiceImpl apiMetadataService;

	public static ApiMetadataWrapper build() {
		return new ApiMetadataWrapper();
	}

	@Override
	public ApiMetadataVO entityVO(ApiMetadata metadata) {
		ApiMetadataVO metadataVO = BeanUtil.copy(metadata, ApiMetadataVO.class);
		assert metadataVO != null;
		if (Func.isEquals(metadata.getGroupId(), BladeConstant.TOP_PARENT_ID)) {
			metadataVO.setRef(BladeConstant.TOP_PARENT_NAME);
		} else {
			ApiMetadata parent = apiMetadataService.getById(metadata.getGroupId());
			if(parent != null){
				metadataVO.setRef(parent.getName());
			}
		}
		return metadataVO;
	}

	public List<ApiMetadataVO> listNodeVOs(List<ApiMetadataVO> list) {
		for (ApiMetadataVO i : list){
			i.setNo(i.getId().toString());
			i.setParentNo(i.getGroupId() == null ? null : i.getGroupId().toString());
			i.setParentId(i.getGroupId() == null ? 0L : i.getGroupId());
		}
		return ForestNodeMerger.merge(list);
	}
	public List<MetadataVO> listNodeStruct(List<MetadataVO> list) {
		for (MetadataVO i : list){
			i.setNo(i.getId().toString());
			i.setParentNo(i.getParentId() == null ? null : i.getParentId().toString());
			i.setParentId(i.getParentId() == null ? 0L : i.getParentId());
		}
		return ForestNodeMerger.merge(list);
	}
	public List<ServerApiMetadataVO> listNodeVO(List<ServerApiMetadataVO> list) {
		for (ServerApiMetadataVO i : list){
			i.setNo(i.getId().toString());
			i.setParentNo(i.getGroupId() == null ? null : i.getGroupId().toString());
		}
		return ForestNodeMerger.merge(list);
	}


}
